<?php
/*
*/
require_once("../include/functions.php");
?>
<html>
  <head>
    <title>JoshHughes.com Hockey Pool</title>
  </head>
<body>
<?php
//Connect To Database
dbConnect();

$today = date("Ymd");
echo "<h2>" . date("l, F jS, Y") . " ($today)" . "</h2>\n";

$Leagues = array("NHL");
foreach ($Leagues as $League)
{
		//Write SQL Query to extract specified data
		$UserSQL = "SELECT ID, RemindMe, Fname, Lname, Email FROM Users WHERE RemindMe > 0 AND ".$League."=1";
		//Open Record Set using SQL Query
		$UserResult = @mysql_query($UserSQL) or die("Cannot query the database.<br/>");
		do
		{
			if (($UserRow["ID"] != NULL) && ($UserRow["ID"] != ""))
			{	
				$GamesString = "";	//Clear GamesString until we find a game that needs picked.
			
				//Select Games that are comming up "RemindMe" days from today...
				$GameSQL = "SELECT Week, GameNum, GameDate, GameTime, Away, Home, PickEMailed FROM ".$League."Schedule WHERE GameDate<='" . (date("Y-m-d",strtotime($today + $UserRow["RemindMe"]))) . "' AND Away <> 'TBD' AND PickEMailed=0 ORDER BY GameDate, GameTime, Week, GameNum";
				//Open Record Set using SQL Query
				$GameResult = @mysql_query($GameSQL) or die("Cannot query the database.<br/>");
				do
				{
					if (($GameRow["GameDate"] != NULL) && ($GameRow["GameDate"] != ""))
					{
						//See if the user made the pick for this game
						$PickSQL = "SELECT Game".$GameRow["GameNum"]." FROM ".$League."Picks WHERE UserID=".$UserRow["ID"]." AND Week=".$GameRow["Week"];
						//Open Record Set using SQL Query
						$PickResult = @mysql_query($PickSQL) or die("Cannot query the database.<br/>");
						$PickRow = @mysql_fetch_array($PickResult);
						if($PickRow["Game".$GameRow["GameNum"]] == NULL) //User did NOT make pick, append information about games to GamesString for Email
							$GamesString .= "\t".$GameRow["Away"]." @ ".$GameRow["Home"]." on " . date("D M jS, Y", strtotime($GameRow["GameDate"])) . " at ".date("g:i A", strtotime($GameRow["GameTime"]))."\n";
					}
				}while($GameRow = @mysql_fetch_array($GameResult));
			}
			//Send Email Reminder
			if ($GamesString != "")
				RemindUser($League, $UserRow["Fname"], $UserRow["Lname"], $UserRow["Email"], $GamesString, $UserRow["RemindMe"]);
			
		}while($UserRow = @mysql_fetch_array($UserResult));
	//set up some email varibables
	$MailFrom = "From: Hockey@joshhughes.com";
	$MailTo = "Hockey@joshhughes.com";
	$MailSubject = "$League Reminder Email: ".date("F jS, Y");
	$MailBody = "The $League reminder email was run today, ".date("F jS, Y")." at ".date("H:i:s");		
	//Send an Email notifying user of games to pick.
	mail($MailTo, $MailSubject, $MailBody, $MailFrom);
}
?>
</body>
</html>
<?
function RemindUser($League, $First, $Last, $Email, $GameString, $RemindMe)
{
//set up some email varibables
$MailFrom = "From: Hockey@joshhughes.com";
$MailTo = "$First $Last <$Email>";
#$MailSubject = "$League Picks reminder for ".date("F jS, Y");
$MailSubject = "$League reminder for Next Round of Series";
$MailBody = "$First, the next round of NHL series & games has been added to the pool.  Don't forget to make your series picks before Game1 of the series!\n
Don't forget to make your picks in the $League pool for the upcoming games:\n$GameString
Good luck,\nHockey Pool Administrtor\n\n
You currently have your profile set to remind you to make picks on games starting within $RemindMe day(s) of today.  This feature is turned on by default at user registration and set to 2 days.  Users may \"Opt Out\" of this reminder feature by setting their preference to 0 (zero) days on the \"Edit Profile\" page.";
	
//Send an Email notifying user of games to pick.
mail($MailTo, $MailSubject, $MailBody, $MailFrom);
echo "MailTO: $MailTo<br/>MailFrom: $MailFrom<br/>MailSubject: $MailSubject<br/>MailBody:<br/>$MailBody<br/>";
}
?>